import axios from 'axios'

const Axios = {
  install(Vue) {
    const instance = axios.create({
      baseURL: "http://localhost:3000"
    })

    instance.interceptors.request.use(
      (config) => {
        // 在发送请求之前做些什么
        config.headers['Authorization'] = 'SomeAuthorization'
        return config;
      },
      (error) => {
        // 对请求错误做些什么
        return Promise.reject(error);
      }
    );

    // 添加响应拦截器
    instance.interceptors.response.use(
      (response) => {
        if (response.data.code === '500') {
          console.error(response.data.msg)
        }
        return response.data;
      },
      (error) => {
        if (error.response.status == 404) {
          console.error('请求不存在')
        }
        return Promise.reject(error);
      }
    );

    Vue.prototype.$axios = instance;
  }
}

export default Axios